package com.ywy.service.impl;

import com.ywy.dao.LinkDao;
import com.ywy.entity.Link;
import com.ywy.service.LinkService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 友情链接Service实现类
 *
 * @author ywy
 * @date 2022/8/25 17:04
 */
@Service
public class LinkServiceImpl implements LinkService {
    @Resource
    private LinkDao linkDao;

    @Override
    public List<Link> listAll() {
        return linkDao.findAll(new Sort(Sort.Direction.ASC, "sort"));
    }

    @Override
    public List<Link> list(Integer page, Integer pageSize) {
        Pageable pageable = new PageRequest(page - 1, pageSize, Sort.Direction.ASC, "sort");
        Page<Link> pageLink = linkDao.findAll(pageable);
        return pageLink.getContent();
    }

    @Override
    public long getTotal() {
        return linkDao.count();
    }

    @Override
    public Link getById(Integer id) {
        return linkDao.findOne(id);
    }

    @Override
    public Link save(Link link) {
        return linkDao.save(link);
    }

    @Override
    public void delete(Integer id) {
        linkDao.delete(id);
    }
}
